﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TestMvc.Controllers
{
  public class DemoController : Controller
  {
    //
    // GET: /Demo/

    public ActionResult Index()
    {
      return View();
    }
    public ActionResult UploadFile()
    {
      return View();
    }
    [HttpPost]
    public ActionResult UploadFile(string type)
    {
      var list = new List<FileModel>();
      for (int i = 0, length = Request.Files.Count; i < length; i++)
      {
        HttpPostedFileBase file = Request.Files[i];
        list.Add(new FileModel
        {
          Name = file.FileName,
          ContentType = file.ContentType,
          ContentLength = file.ContentLength
        });
      }
      if (Request.Headers["X-Requested-With"] != null)
      {
        return Json(new { Files = list, type = type });
      }
      Response.Write("<textarea>");
      new JsonResult { Data = new { Files = list, type = type } }.ExecuteResult(ControllerContext);
      Response.Write("</textarea>");
      Response.ContentType = "text/html";
      return Content(string.Empty);
    }
  }

  public class FileModel
  {
    public string Name { get; set; }
    public string ContentType { get; set; }
    public int ContentLength { get; set; }
  }
}
